package com.SpringMVC.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.SpringMVC.model.TPersonMessage;
import com.SpringMVC.model.TUser;
import com.SpringMVC.service.IPersonMessage;
@Controller
@RequestMapping("/PersonMessage")
public class PersonMessage {
	
	private IPersonMessage personMessage;
	private HttpSession session;

	public IPersonMessage getPersonMessage() {
		return personMessage;
	}
	@Autowired
	public void setPersonMessage(IPersonMessage personMessage) {
		this.personMessage = personMessage;
	}
	
	//获取用户的所有信息
	@RequestMapping("/getAllPersonMessage")
	public String getAllPersonMessage(TPersonMessage tpm,HttpSession session){
		
		TUser t=(TUser) session.getAttribute("tUser");
		//System.out.println(t);
		String tUserName=t.gettUserName();
		TPersonMessage tp=personMessage.selectAllByName(tUserName);
		session.setAttribute("tPerson", tp);
		return "admin-user";
	}
	
	public TPersonMessage getPersonMessageMethod(TPersonMessage tpm,HttpSession session){
		TUser t=(TUser) session.getAttribute("tUser");
		
		String tUserName=t.gettUserName();
		TPersonMessage tp=personMessage.selectAllByName(tUserName);
		return tp;
	}
	
	@RequestMapping("/insertOrUpdate")
	public String insertOrUpdateMessage(TPersonMessage tpm,HttpSession session){
		//取得查询结果如果用户名为空即为插入
		TPersonMessage itp=getPersonMessageMethod(tpm,session);
		System.out.println(tpm.gettName());
		if(itp==null||itp.gettUserName()==null){
			TUser itm=(TUser) session.getAttribute("tUser");
			
			String tUserName=itm.gettUserName();
			tpm.settUserName(tUserName);
			personMessage.insertPersonMessage(tpm);			
		}else{
			personMessage.updatepersonMessage(tpm);;
			
		}
		TPersonMessage stpm=getPersonMessageMethod(tpm,session);
		session.setAttribute("tPerson", stpm);
		return "admin-user";
		
	}
	
	

}
